from sqlalchemy.orm import Session

from module_admin.dao.cms_car_team_dao import CmsCarTeamDao
from module_admin.entity.vo.cms_car_team_vo import CarTeamModel, CarTeamForQuery
from module_admin.entity.vo.cms_car_vo import CrudCarResponse
from utils.uuid import UUID


class CmsCarTeamService:

    @classmethod
    def create_car_team(cls, result_db: Session, page_object: CarTeamModel):
        uuid = UUID().uuid()
        page_object.id = int(uuid)
        page_object.car_team_id = "tea_" + uuid
        try:
            CmsCarTeamDao.create_car_team(result_db, page_object)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))

        return CrudCarResponse(**result)

    @classmethod
    def delete_car_team(cls, query_db: Session, id: int):
        CmsCarTeamDao.delete_car_team_by_id(query_db, id)
        query_db.commit()

    @classmethod
    def get_car_team_by_id(cls, query_db: Session, id: int):
        team_by_id = CmsCarTeamDao.get_car_team_by_id(query_db, id)
        return team_by_id

    @classmethod
    def list(cls, db: Session, query_object: CarTeamForQuery):
        car_list_result = CmsCarTeamDao.list(db, query_object)
        return car_list_result


